<template>
  <!--
    	作者：wangxw
    	时间：2019-10-26
    	描述：产品分类管理
    -->
  <div class="product">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="使用中" name="1">
          <span slot="label">
            使用中
            <el-tag size="mini">{{ category_count.using }}</el-tag>
          </span>
        </el-tab-pane>
        <el-tab-pane label="待审核" name="0">
          <span slot="label">
            待审核
            <el-tag size="mini">{{ category_count.audit }}</el-tag>
          </span>
        </el-tab-pane>
        <el-tab-pane label="未通过" name="2">
          <span slot="label">
            未通过
            <el-tag size="mini">{{ category_count.no_pass }}</el-tag>
          </span>
        </el-tab-pane>
      </el-tabs>
      <div class="mb16">
        <div class="common-level-rail fl">
          <el-button
            v-if="activeName != 1"
            :disabled="this.multipleSelection.length == 0"
            size="small"
            type="primary"
            icon="el-icon-coin"
            @click="batchRecommend"
            >批量审核</el-button
          >
        </div>
      </div>
    </div>

    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          style="width: 100%"
          v-loading="loading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            v-if="Number(activeName) != 1"
            key="2"
            type="selection"
            width="55"
          />
          <el-table-column
            prop="alias"
            label="分类名称"
            width="180"
          ></el-table-column>
          <el-table-column
            prop="category_id"
            label="分类ID"
            width="100"
          ></el-table-column>
          <el-table-column prop="" label="图片" width="180">
            <template slot-scope="scope">
              <img v-img-url="hasImages(scope.row.image)" alt="" width="50px" />
            </template>
          </el-table-column>
          <el-table-column prop="sort" label="分类排序"></el-table-column>
          <el-table-column
            prop="create_time"
            label="添加时间"
          ></el-table-column>
          <el-table-column prop="proxy_id" label="代理商">
            <template slot-scope="scope">
              {{ scope.row.proxy ? scope.row.proxy.name : "--" }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.audit_status != 1"
                @click="auditClick(scope.row)"
                type="text"
                size="small"
                >审核</el-button
              >
              <el-button
                @click="deleteClick(scope.row)"
                type="text"
                size="small"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <!--分页-->
        <div class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            background
            :current-page="curPage"
            :page-size="pageSize"
            layout="total, prev, pager, next, jumper"
            :total="totalDataNumber"
          ></el-pagination>
        </div>
      </div>
    </div>

    <!--添加-->
    <Add
      v-if="open_add"
      :open_add="open_add"
      :addform="categoryModel"
      @closeDialog="closeDialogFunc($event, 'add')"
    ></Add>
    <!--修改-->
    <Edit
      v-if="open_edit"
      :open_edit="open_edit"
      :editform="categoryModel"
      @closeDialog="closeDialogFunc($event, 'edit')"
    ></Edit>
    <!--审核-->
    <Audit
      v-if="open_audit"
      :open_audit="open_audit"
      :auditform="categoryModel.model"
      :title="title"
      :AuditApi="AuditApi"
      :pk="pk"
      :ids="batchIds"
      @closeDialog="closeDialogFunc($event, 'audit')"
    ></Audit>
  </div>
</template>

<script>
import Add from "./Add.vue";
import Edit from "./Edit.vue";
import Audit from "@/components/audit/Audit.vue";
import ProxyCategoryApi from "@/api/proxyCategory.js";
export default {
  components: {
    Add,
    Edit,
    Audit
  },
  data() {
    return {
      /*切换菜单*/
      activeName: "1",
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*是否打开审核弹窗*/
      open_audit: false,
      /*当前编辑的对象*/
      categoryModel: {
        catList: [],
        model: {}
      },
      /*分类统计*/
      category_count: {},
      pk: "id",
      title: "分类审核",
      /*接口*/
      AuditApi: ProxyCategoryApi,
      batchIds: [],
      /*选中的数据*/
      multipleSelection: []
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    hasImages(e) {
      if (e) {
        return e.file_path;
      } else {
        return "";
      }
    },
    /*获取列表*/
    getData() {
      let self = this;
      let Params = {};
      Params.audit_status = self.activeName;
      self.loading = true;

      ProxyCategoryApi.catList(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.list.data;
          self.totalDataNumber = data.data.list.total;
          self.category_count = data.data.category_count;
        })
        .catch(error => {
          self.loading = false;
        });
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },
    //记录选中数值
    handleSelectionChange(val) {
      this.multipleSelection = val;
      const data = [];
      this.multipleSelection.map(item => {
        data.push(item[this.pk]);
      });
      this.batchIds = data;
    },
    batchRecommend() {
      this.userModel = {};
      this.open_audit = true;
    },
    /*切换菜单*/
    handleClick(tab, event) {
      let self = this;
      self.curPage = 1;
      self.getData();
    },
    /*打开添加*/
    addClick() {
      this.open_add = true;
    },

    /*打开编辑*/
    editClick(item) {
      this.categoryModel.model = item;
      this.open_edit = true;
    },

    /*打开审核*/
    auditClick(item) {
      this.categoryModel.model = item;
      this.open_audit = true;
    },

    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "add") {
        this.open_add = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
      if (f == "audit") {
        this.open_audit = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
    },
    /*删除分类*/
    deleteClick(row) {
      let self = this;
      self
        .$confirm("删除后不可恢复，确认删除该记录吗?", "提示", {
          type: "warning"
        })
        .then(() => {
          ProxyCategoryApi.catDel({
            id: row.id
          }).then(data => {
            self.$message({
              message: "删除成功",
              type: "success"
            });
            self.getData();
          });
        });
    }
  }
};
</script>

<style></style>
